package com.yuqih.common.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.Set;

import org.apache.commons.beanutils.PropertyUtils;

import com.yuqih.common.config.IFrameworkConfig;
import com.yuqih.common.exception.CodedException;

class DefaultConvertBuilder extends AbstractConvertBuilder<Object, Object> {
		
	/**
	 * @param modelType
	 * @param entityType
	 * @param frameworkConfig
	 */
	DefaultConvertBuilder(Class<?> sourceType, Class<?> targetType, IFrameworkConfig frameworkConfig) {
		super(sourceType, targetType, frameworkConfig);
	}

	@Override
	protected void build(Object source, Object target, String field) {
		Set<String> targetFields = targetDescriptor.getFieldNames();
		if (!targetFields.contains(field)) {
			return;
		}
		try {
			PropertyUtils.setProperty(target, field, PropertyUtils.getProperty(source, field));
		} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
			throw CodedException.from(e);
		}
		
	}

}
